package com.flea.web.controller;

import com.flea.def.Definition;
import com.flea.model.Product;
import com.flea.model.Result;
import com.flea.model.User;
import com.flea.service.UploadFileService;
import com.flea.service.UserService;
import com.flea.util.SessionUtils;
import com.flea.web.vo.datatables.DTInput;
import com.flea.web.vo.datatables.DTOutput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author hc24
 * @date 2021.1.20 13:16
 */
@Slf4j
@Controller
public class UserController {
    @Resource
    private UserService userService;

    @Resource
    private UploadFileService uploadFileService;

    @ResponseBody
    @PostMapping("/login")
    public Result login(String login, String pwd, HttpSession session) {
        try {
            User user = userService.login(login, pwd);
            session.setAttribute(Definition.USER_SESSION_KEY, user);
            return Result.success();
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        // 使会话失效
        session.invalidate();
        return "login";
    }

    @PostMapping("/register")
    public String register(User user, MultipartFile headFile, Model model) {
        try {
            if (!headFile.isEmpty()) {
                if (!headFile.getContentType().startsWith("image/")) {
                    model.addAttribute("msg", "头像非法");
                    return "error";
                }
                Integer head = uploadFileService.save(headFile);
                user.setHead(head);
            }
            userService.register(user);
        } catch (IOException e) {
            log.warn("io error", e);
            model.addAttribute("msg", "报错头像出错，请稍后重试");
            return "error";
        } catch (Exception e) {
            log.warn("register error", e);
            model.addAttribute("msg", e.getMessage());
            return "error";
        }
        return "login";
    }

    @ResponseBody
    @PostMapping("/registerAjax")
    public Result registerAjax(User user, MultipartFile headFile) {
        try {
            if (headFile != null && !headFile.isEmpty()) {
                if (!headFile.getContentType().startsWith("image/")) {
                    return Result.fail("头像非法");
                }
                Integer head = uploadFileService.save(headFile);
                user.setHead(head);
            }
            userService.register(user);
            return Result.success();
        } catch (IOException e) {
            log.warn("io error", e);
            return Result.fail("上传头像出错，请稍后重试");
        } catch (Exception e) {
            log.warn("register error", e);
            return Result.fail(e.getMessage());
        }
    }

    @GetMapping("/user/profile")
    public String profile(HttpSession session) {
        return "user/profile";
    }

    @ResponseBody
    @GetMapping("/user/products")
    public DTOutput products(String key, Integer minPrice, DTInput dtInput, HttpSession session) {
        Page<Product> page = userService.findProduct(SessionUtils.getSessionUser(session).getId(), key, minPrice, dtInput.toPageRequest());
        return DTOutput.of(dtInput, page);
    }

}
